package com.capinfo.accounting.web;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.capinfo.accumulation.model.accounting.mybist.GB_JZPZXX;
import com.capinfo.accumulation.model.accounting.mybist.ZDPZCWPZMB;
import com.capinfo.accumulation.model.accounting.mybist.ZDPZFZZPZMB;
import com.capinfo.accumulation.model.accounting.mybist.ZDPZMBJBSJ;
import com.capinfo.accumulation.model.enums.JkfhztEnums;
import com.capinfo.accumulation.model.enums.JzpzbjEnums;
import com.capinfo.accumulation.model.enums.PzZfEnums;
import com.capinfo.accumulation.model.pagemodel.InterfaceRetBean;
import com.capinfo.accumulation.parameter.accounting.DataGrid;
import com.capinfo.accumulation.service.accounting.mybits.GB_JZPZXXServiceI;
import com.capinfo.accumulation.service.accounting.mybits.HZJZPZServiceI;
import com.capinfo.accumulation.service.accounting.mybits.ZDPZCWPZMBServiceI;
import com.capinfo.accumulation.service.accounting.mybits.ZDPZFZZPZMBServiceI;
import com.capinfo.accumulation.service.accounting.mybits.ZDPZMBJBSJServiceI;
import com.capinfo.accumulation.util.DateUtil;

/**
 * 	自动凭证接口
 *  @author zxl
 */
@Controller
@RequestMapping("/business/accounting/zdpzinterface")
public class ZdpzInterfaceController extends BaseController {
	private static final String ZDPZMBJBSJ = null;
	
	
	@Autowired
	public GB_JZPZXXServiceI gb_JZPZXXService;
	@Autowired
	public HZJZPZServiceI hzjzpzServiceI;
	/**
	 * 自动凭证模版基本数据
	 */
	@Autowired
	public ZDPZMBJBSJServiceI zDPZMBJBSJService;
	/**
	 * 自动凭证财务凭证模版
	 */
	@Autowired
	public ZDPZCWPZMBServiceI  zDPZCWPZMBService;
	/**
	 * 字段凭证辅助账凭证模版
	 */
	@Autowired
	public ZDPZFZZPZMBServiceI  zDPZFZZPZMBService;

	@RequestMapping("/interface.shtml")
	@ResponseBody
	public  InterfaceRetBean zdpcjk(@RequestBody  Map param) throws ParseException {
		//1 数据有效性
		//3生成汇总凭证
		//4生成记账凭证
		//5生成辅助凭证
		//验证消息头
		InterfaceRetBean ret = yzhead(param);
		if(ret.getZT() == JkfhztEnums.失败.getToken()){//消息头验证失败
			return ret;
		}
		ret =  yzxxbody(param);
		if(ret.getZT() == JkfhztEnums.失败.getToken()){//消息头验证失败
			return ret;
		}
		Map map  = new HashMap();
		map.put("YWLX_EQ_STR", param.get("YWLX"));
		map.put("YWZLX_EQ_STR", param.get("YWZLX"));
		DataGrid zdpzmbGrid = zDPZMBJBSJService.findbyByGrid(map);
		ZDPZMBJBSJ zdpzmbjbsj = (ZDPZMBJBSJ)zdpzmbGrid.getRows().get(0);
		Map mappzmp = new  HashMap();
		mappzmp.put("ZDPZMBID_EQ_NUM", zdpzmbjbsj.getID());
		List<ZDPZCWPZMB> zDPZCWPZMBList = zDPZCWPZMBService.findbyByGrid(mappzmp).getRows();
		Map body = (Map)param.get("PARM");
		for(int i = 0 ; i < zDPZCWPZMBList.size() ; i++){
			ZDPZCWPZMB  zdpzcwpzmb = zDPZCWPZMBList.get(i);
			GB_JZPZXX jzpzxx = new GB_JZPZXX();
			jzpzxx.setZHAIYAO(zdpzcwpzmb.getZY());
			jzpzxx.setJZPZH("00001");
			jzpzxx.setKMBH(zdpzcwpzmb.getKMBM());
			if(zdpzcwpzmb.getJDFX() .equals("借")){
				jzpzxx.setJFFSE(Double.parseDouble(body.get(zdpzcwpzmb.getQZFS()).toString()));
				jzpzxx.setDFFSE(0.0);
			}else{
				jzpzxx.setJFFSE(0.0);
				jzpzxx.setDFFSE(Double.parseDouble(body.get(zdpzcwpzmb.getQZFS()).toString()));
			}
			jzpzxx.setFJDJS(1L);
			jzpzxx.setPZRQ(DateUtil.getDate(body.get("YWRQ").toString(), "yyyy-MM-dd") );
			jzpzxx.setYWLX(param.get("YWLX").toString());
			jzpzxx.setZJYWLX(param.get("ZJYWLX").toString());
			jzpzxx.setZD(body.get("ZD").toString());
			jzpzxx.setZDRQ(DateUtil.getDate(body.get("YWRQ").toString(), "yyyy-MM-dd") );
			jzpzxx.setPZLB(JzpzbjEnums.自动凭证.toString());
			jzpzxx.setZTDM(zdpzmbjbsj.getZTMC());
			//jzpzxx.setHSDW(zdpzmbjbsj.getHSDW());
			jzpzxx.setZZBS("自动凭证");
			jzpzxx.setFLXH(zdpzcwpzmb.getFLXH());
			jzpzxx.setZFZT(PzZfEnums.正常.toString());
		}
		
		
		return ret;
	}
	
	/**
	 * 验证消息头
	 * @param param
	 * @return
	 */
	private InterfaceRetBean  yzhead(Map param ) {
		InterfaceRetBean json = new InterfaceRetBean();
		json.setZT(JkfhztEnums.成功.getToken());
		if(param.get("YWLX")==null){
			json.setZT(JkfhztEnums.失败.getToken());
			json.setMsg("业务类型不能为空");
			return json;
		}
		if(param.get("YWZLX")==null){
			json.setZT(JkfhztEnums.失败.getToken());
			json.setMsg("业务子类型不能为空");
			return json;
		}
		if(param.get("YWLSH")==null){
			json.setZT(JkfhztEnums.失败.getToken());
			json.setMsg("业务流水不能为空");
			return json;
		}
		if(param.get("YHCKRJZID")==null){
			json.setZT(JkfhztEnums.失败.getToken());
			json.setMsg("银行存款日记账ID不能为空");
			return json;
		}
		Map map  = new HashMap();
		map.put("YWLX_EQ_STR", param.get("YWLX"));
		map.put("YWZLX_EQ_STR", param.get("YWZLX"));
		DataGrid zdpzmbGrid = zDPZMBJBSJService.findbyByGrid(map);
		ZDPZMBJBSJ zdpzmbjbsj = (ZDPZMBJBSJ)zdpzmbGrid.getRows().get(0);
		if(zdpzmbGrid.getRows().size() == 0){
			json.setZT(JkfhztEnums.失败.getToken());
			json.setMsg("调用的凭证模版不存在");
			return json;
		}
		return json ;
	}
	/**
	 * 验证消息体
	 * @param param
	 * @return
	 */
	private InterfaceRetBean yzxxbody(Map param) {
		InterfaceRetBean json = new InterfaceRetBean();
		json.setZT(JkfhztEnums.成功.getToken());
		Map map  = new HashMap();
		map.put("YWLX_EQ_STR", param.get("YWLX"));
		map.put("YWZLX_EQ_STR", param.get("YWZLX"));
		DataGrid zdpzmbGrid = zDPZMBJBSJService.findbyByGrid(map);
		ZDPZMBJBSJ zdpzmbjbsj = (ZDPZMBJBSJ)zdpzmbGrid.getRows().get(0);
		Map mappzmp = new  HashMap();
		mappzmp.put("ZDPZMBID_EQ_NUM", zdpzmbjbsj.getID());
		List<ZDPZCWPZMB> zDPZCWPZMBList = zDPZCWPZMBService.findbyByGrid(mappzmp).getRows();
		Map body = (Map)param.get("PARM");
		for(int i = 0 ; i < zDPZCWPZMBList.size() ; i++){
			ZDPZCWPZMB tmp = zDPZCWPZMBList.get(i);
			if(body.get(tmp.getQZFS()) == null){
				json.setZT(JkfhztEnums.失败.getToken());
				json.setMsg("消息体中参数缺失");
				return json;
			}
		}
		List< ZDPZFZZPZMB>  zDPZFZZPZMBList  = zDPZCWPZMBService.findbyByGrid(mappzmp).getRows();
		for(int i = 0 ; i <zDPZFZZPZMBList.size(); i ++){
			ZDPZFZZPZMB tmp = zDPZFZZPZMBList.get(i);
			if(body.get(tmp.getQZFS()) == null){
				json.setZT(JkfhztEnums.失败.getToken());
				json.setMsg("消息体中参数缺失");
				return json;
			}
			if(body.get(tmp.getYYMCGS()) == null){
				json.setZT(JkfhztEnums.失败.getToken());
				json.setMsg("消息体中参数缺失");
				return json;
			}
			if(body.get(tmp.getEYMCGS()) == null){
				json.setZT(JkfhztEnums.失败.getToken());
				json.setMsg("消息体中参数缺失");
				return json;
			}
		}
		return json;
	}
	
}
